package cn.edu.nuc.bre.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.nuc.bre.dao.AnnouncementMapper;
import cn.edu.nuc.bre.dao.QueryMapper;
import cn.edu.nuc.bre.dto.QueryHandler;
import cn.edu.nuc.bre.entity.Announcement;
import cn.edu.nuc.bre.service.interfaces.AnnouncementService;
import cn.edu.nuc.bre.util.PageInfo;

/**
 * @author Mine
 * @since 2016年11月9日_下午9:44:07
 */
@Service
public class AnnouncementServiceImpl implements AnnouncementService {
	@Autowired
	private AnnouncementMapper am;
	@Autowired
	private QueryMapper qm;

	@Override
	public boolean add(Announcement one) {
		if (one != null) {
			one.setAnnoid(null);// 不需要id
			one.setAnnostate(1);//设置默认状态为1
			one.setAnnotime(new Date());// 设置时间[时区问题没有解决。。。]
			if (am.insertSelective(one) == 1) {
				return true;
			} else {
				System.out.println("数据库添加操作失败...");
			}
		} else {
			System.out.println("公告参数为空...");
		}
		return false;
	}

	@Override
	public boolean delete(Integer annoid) {
		if (annoid != null) {
			Announcement one = new Announcement();
			one.setAnnoid(annoid);
			one.setAnnostate(0);// 0表示删除
			if (am.updateByPrimaryKeySelective(one) == 1) {
				return true;
			} else {
				System.out.println("数据库[删除公共]操作失败...");
			}
		} else {
			System.out.println("删除公告id为空...");
		}
		return false;
	}

	@Override
	public Announcement findById(Integer annoid) {
		// TODO 自动生成的方法存根
		if (annoid != null) {
			return am.selectByPrimaryKey(annoid);
		} else {
			System.out.println("id为空");
			return null;
		}

	}

	@Override
	public PageInfo<Announcement> findByQueryHandler(QueryHandler qh) {
		List<Announcement> list = qm.queryAnnouncement(qh);
		long rootSize = 0;
		int nowPage = 1;
		int pageSize = 5;
		if (qh == null || qh.getLimitSentence() == null) {// 没分页
			if (list != null) {
				rootSize = list.size();
			}
		} else {
			nowPage = qh.getPageNumber();
			pageSize = qh.getPageSize();
			rootSize = qm.findLastRows();
		}
		/*
		 * System.out.println(rootSize); System.out.println(nowPage);
		 * System.out.println(pageSize);
		 */
		PageInfo<Announcement> pi = new PageInfo<Announcement>((int) rootSize, nowPage, pageSize);
		// System.out.println(pi);
		pi.setBasePageList(list);
		return pi;
	}

}
